/**  
 * Project Name:api-webhook  
 * File Name:ChsiService.java  
 * Package Name:com.example.service.webhook.business.chsi.service  
 * Date:2016年7月26日下午4:07:19  
 * Copyright (c) 2016, yuandong@51dojo.com All Rights Reserved.  
 *  
*/

package com.example.service.webhook.business.alipay.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import com.example.service.webhook.business.alipay.api.AlipayClient;
import com.example.service.webhook.business.alipay.billitem.AlipayBillTask;
import com.example.service.webhook.business.alipay.dto.AlipayTradeCategory;
import com.example.service.webhook.business.alipay.dto.AlipayTradeRecord;
import com.example.service.webhook.business.alipay.dto.AlipayUserInfo;
import com.example.service.webhook.business.alipay.dto.AlipayWealth;
import com.example.service.webhook.business.alipay.dto.AlipayYuebaoTradeInfo;
import com.example.service.webhook.business.alipay.entity.AlipayTradeCategoryEntity;
import com.example.service.webhook.business.alipay.entity.AlipayTradeRecordEntity;
import com.example.service.webhook.business.alipay.entity.AlipayUserInfoEntity;
import com.example.service.webhook.business.alipay.entity.AlipayWealthEntity;
import com.example.service.webhook.business.alipay.entity.AlipayYuebaoTradeInfoEntity;
import com.example.service.webhook.business.alipay.repository.AlipayTradeCategoryRepository;
import com.example.service.webhook.business.alipay.repository.AlipayTradeRecordRepository;
import com.example.service.webhook.business.alipay.repository.AlipayUserInfoRepository;
import com.example.service.webhook.business.alipay.repository.AlipayWealthRepository;
import com.example.service.webhook.business.alipay.repository.AlipayYuebaoTradeInfoRepository;

/**
 * ClassName:ChsiService <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2016年7月26日 下午4:07:19 <br/>
 * 
 * @author yuandong
 * @version
 * @since JDK 1.6
 * @see
 */
@Service
public class AlipayService {

	private static final Logger LOGGER = LoggerFactory.getLogger(AlipayService.class);

	@Autowired
	private ThreadPoolTaskExecutor taskExecutor;
	@Autowired
	private AlipayClient alipayClient;
	@Autowired
	private AlipayUserInfoRepository alipayUserInfoRepository;
	@Autowired
	private AlipayTradeRecordRepository alipayTradeRecordRepository;
	@Autowired
	private AlipayWealthRepository alipayWealthRepository;
	@Autowired
	private AlipayYuebaoTradeInfoRepository alipayYuebaoTradeInfoRepository;
	@Autowired
	private AlipayTradeCategoryRepository alipayTradeCategoryRepository;

	/**
	 * 
	 * fetchBill:处理支付宝回调 <br/>
	 * 在收到魔蝎的邮箱账单回调时执行<br/>
	 * 学籍信息<br/>
	 * 学历信息<br/>
	 * 
	 * @author yuandong
	 * @param task
	 * @since JDK 1.6
	 */
	public void fetchBill(final AlipayBillTask task) {

		// 这里交给线程池处理，防止下面的业务处理时间太长，导致超时。
		// 超时会导致魔蝎数据进行重试，会收到重复的回调请求

		taskExecutor.execute(new Runnable() {
			@Override
			public void run() {
				saveUserInfo(task);
				saveTradeInfo(task);
				saveWealth(task);
//				saveYuebaoTradeInfo(task);
//				saveTradeCategory(task);

			}
		}

		);

	}

	private void saveUserInfo(AlipayBillTask task) {
		try {
			AlipayUserInfo userinfo = alipayClient.getUserInfo(task.getTaskId());
			if (userinfo != null) {
				AlipayUserInfoEntity alipayUserInfoEntity = new AlipayUserInfoEntity();
				BeanUtils.copyProperties(userinfo, alipayUserInfoEntity);
				alipayUserInfoEntity.setUserId(task.getUserId());
				AlipayUserInfoEntity isExistEntity = alipayUserInfoRepository.getUserInfo(task.getUserId(),
						task.getMappingId());
				if (isExistEntity != null) {
					alipayUserInfoEntity.setCreateTime(isExistEntity.getCreateTime());
					alipayUserInfoEntity.setId(isExistEntity.getId());
					alipayUserInfoRepository.updateUserInfo(alipayUserInfoEntity);
				} else {
					alipayUserInfoRepository.saveUserInfo(alipayUserInfoEntity);
				}
			}
		} catch (Exception e) {
			LOGGER.error("处理支付宝基本信息出现异常", e);
		}

	}

	private void saveTradeInfo(AlipayBillTask task) {
		try {
			List<AlipayTradeRecord> tradeRecords = alipayClient.getTradeInfo(task.getTaskId());
			if (tradeRecords != null) {
				for (AlipayTradeRecord alipayTradeRecord : tradeRecords) {
					AlipayTradeRecordEntity alipayTradeRecordEntity = new AlipayTradeRecordEntity();
					BeanUtils.copyProperties(alipayTradeRecord, alipayTradeRecordEntity);
					alipayTradeRecordEntity.setUserId(task.getUserId());
					AlipayTradeRecordEntity isExistEntity = alipayTradeRecordRepository.getTradeRecord(task.getUserId(),
							task.getMappingId(), alipayTradeRecord.getTradeNumber());
					if (isExistEntity != null) {
						// alipayTradeRecordEntity.setCreateTime(isExistEntity.getCreateTime());
						// alipayTradeRecordEntity.setId(isExistEntity.getId());
						// alipayTradeRecordRepository.updateTradeRecord(alipayTradeRecordEntity);
					} else {
						alipayTradeRecordRepository.saveTradeRecord(alipayTradeRecordEntity);
					}
				}

			}
		} catch (Exception e) {
			LOGGER.error("处理支付宝交易记录出现异常", e);
		}

	}

	private void saveWealth(AlipayBillTask task) {
		try {
			AlipayWealth alipayWealth = alipayClient.getWealth(task.getTaskId());
			if (alipayWealth != null) {
				AlipayWealthEntity alipayWealthEntity = new AlipayWealthEntity();
				BeanUtils.copyProperties(alipayWealth, alipayWealthEntity);
				alipayWealthEntity.setUserId(task.getUserId());
				AlipayWealthEntity isExistEntity = alipayWealthRepository.getWealth(task.getUserId(),
						task.getMappingId());
				if (isExistEntity != null) {
					alipayWealthEntity.setCreateTime(isExistEntity.getCreateTime());
					alipayWealthEntity.setId(isExistEntity.getId());
					alipayWealthRepository.updateWealth(alipayWealthEntity);
				} else {
					alipayWealthRepository.saveWealth(alipayWealthEntity);
				}
			}
		} catch (Exception e) {
			LOGGER.error("处理支付宝资产状况出现异常", e);
		}

	}

	/*private void saveYuebaoTradeInfo(AlipayBillTask task) {
		try {
			List<AlipayYuebaoTradeInfo> yuebaoTradeInfos = alipayClient.getYuebaotradeinfo(task.getTaskId());
			if (yuebaoTradeInfos != null) {
				for (AlipayYuebaoTradeInfo alipayYuebaoTradeInfo : yuebaoTradeInfos) {
					AlipayYuebaoTradeInfoEntity alipayYuebaoTradeInfoEntity = new AlipayYuebaoTradeInfoEntity();
					BeanUtils.copyProperties(alipayYuebaoTradeInfo, alipayYuebaoTradeInfoEntity);
					alipayYuebaoTradeInfoEntity.setUserId(task.getUserId());
					AlipayYuebaoTradeInfoEntity isExistEntity = alipayYuebaoTradeInfoRepository
							.getAlipayYuebaoTradeInfo(task.getUserId(), task.getMappingId(),
									alipayYuebaoTradeInfo.getBillCycle());
					if (isExistEntity != null) {
						// alipayTradeRecordEntity.setCreateTime(isExistEntity.getCreateTime());
						// alipayTradeRecordEntity.setId(isExistEntity.getId());
						// alipayTradeRecordRepository.updateTradeRecord(alipayTradeRecordEntity);
					} else {
						alipayYuebaoTradeInfoRepository.saveAlipayYuebaoTradeInfo(alipayYuebaoTradeInfoEntity);
					}
				}

			}
		} catch (Exception e) {
			LOGGER.error("处理支付宝余额宝交易情况出现异常", e);

		}
	}
	
	
	private void saveTradeCategory(AlipayBillTask task) {
		try {
			List<AlipayTradeCategory> tradeCategorys = alipayClient.getTradecategory(task.getTaskId());
			if (tradeCategorys != null) {
				alipayTradeCategoryRepository.deleteAlipayTradeCategory(task.getUserId(), task.getMappingId());
				for (AlipayTradeCategory alipayTradeCategory : tradeCategorys) {
					AlipayTradeCategoryEntity alipayTradeCategoryEntity = new AlipayTradeCategoryEntity();
					BeanUtils.copyProperties(alipayTradeCategory, alipayTradeCategoryEntity);
					alipayTradeCategoryEntity.setUserId(task.getUserId());
					alipayTradeCategoryRepository.saveAlipayTradeCategory(alipayTradeCategoryEntity);
				}

			}
		} catch (Exception e) {
			LOGGER.error("处理支付宝账单周期消费分类信息出现异常", e);

		}
	}*/

}
